//
//  ThreeViewController.m
//  代号：47
//
//  Created by 杨佩璋 on 15/7/7.
//  Copyright (c) 2015年 杨佩璋. All rights reserved.
//

#import "ThreeViewController.h"
#import "ThreeModel.h"
#import <UIImageView+WebCache.h>
#import "AllViewController.h"


@interface ThreeViewController ()
{
    MBProgressHUD *hud;
}
@property (nonatomic,retain) UIScrollView *scroll;
@property (nonatomic,retain)AFHTTPRequestOperation *operation; // 操作。
@property (nonatomic,retain) NSOperationQueue *queue;  // 操作队列
@property (nonatomic,retain) NSMutableArray *imagesModelArr; // 图片数组
@property (nonatomic,retain) NSString *content;

@end

@implementation ThreeViewController

-(void)dealloc{
    [_mytitle release];
    [_imagesModelArr release];
    [_newsId release];
    [_operation release];
    [_queue release];
    [_scroll release];
    [super dealloc];
}


// 加载进度条
- (void)createdownLoadProgress{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.color = [UIColor blackColor];
    [self.view addSubview:hud];
    hud.labelText = @"努力加载中..";
    //设置模式为环状进度提示框
    [hud show:YES];
    [hud release];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    [self createTitle];
    self.imagesModelArr = [NSMutableArray array];
    [self createdownLoadProgress];
    [self requestData];
}


// 创建头文字
- (void)createTitle{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreen_width - 120, 30)];
    label.text = _mytitle;
    label.font = [UIFont systemFontOfSize:12];
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
    [label release];
}


#warning 内存?
//创建scroll
- (void)createScroll{
    _scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.scroll];
    _scroll.showsHorizontalScrollIndicator = NO;
    //    [_scroll release];
}


// AFN 数据请求
- (void)requestData
{
    NSString *str = [NSString stringWithFormat:@"http://api.m.mtime.cn/News/Detail.api?newsId=%@",self.newsId];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    self.operation = operation;
    [operation release];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSArray *arr = dic[@"images"];
        self.content = dic[@"content"];
        for (NSDictionary *dic in arr) {
            ThreeModel *model = [[ThreeModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.imagesModelArr addObject:model];
            [model release];
        }
        [self addImages];
        
    } failure:nil];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
    self.queue = queue;
    [queue release];
}



#pragma mark - 添加图片
- (void)addImages{
    [hud hide:YES];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
    [self createScroll];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.scroll.contentSize = CGSizeMake(self.imagesModelArr.count * kScreen_width, kScreen_height - 64);
    self.scroll.pagingEnabled = YES;
    for (ThreeModel *model in self.imagesModelArr) {
        NSInteger index = [self.imagesModelArr indexOfObject:model];
        
        // 创建黑色View
        UIView *blackBottomView = [[UIView alloc] initWithFrame:CGRectMake(index *kScreen_width, kScreen_height - 30, kScreen_width, 30)];
        blackBottomView.backgroundColor = [UIColor blackColor];
        
        
        // 添加底部描述
        UILabel *label = [[UILabel alloc] init];
        label.width = kScreen_width - 100;
        label.numberOfLines = 1;
        label.x = 20;
        label.height = 30;
        label.y =0;
        label.text = model.desc;
        label.textColor = [UIColor lightGrayColor];
        label.font = [UIFont systemFontOfSize:12];
        label.textAlignment = NSTextAlignmentLeft;
        [blackBottomView addSubview:label];
        [label release];
        
        // 查看全文
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:@"→查看全文" forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //        button.titleEdgeInsets = UIEdgeInsetsMake(0, kScreen_width - 70, 0, 0);
        button.frame = CGRectMake( (kScreen_width - 110) , 0,100, 30);
        [button addTarget:self action:@selector(showAll) forControlEvents:UIControlEventTouchUpInside];
        [blackBottomView addSubview:button];
        
        UIImageView *imageView =  [[UIImageView alloc] init];
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(blackView:)];
        [imageView addGestureRecognizer:tapGest];
        [tapGest release];
        imageView.backgroundColor = [UIColor redColor];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.url1]];
        
        if (!imageView.image) {
            [imageView addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionNew context:nil];
        }
        
        
        CGFloat width = imageView.image.size.width;
        CGFloat height = imageView.image.size.height;
        imageView.x = index *kScreen_width;
        if (width != 0) {
            imageView.width = kScreen_width;
            imageView.height = kScreen_width * height / width;
            imageView.y = (kScreen_height - imageView.height) /2 ;
        }
        
        
        [self.scroll addSubview:imageView];
        [self.scroll addSubview:blackBottomView];
        [blackBottomView release];
        [imageView release];
        
        
    }
    
    
    
}



#pragma mark - 显示全文
- (void)showAll{
    AllViewController *all = [[AllViewController alloc] init];
    all.mytitle = self.mytitle;
    all.modelArr = self.imagesModelArr;
    all.content = self.content;
    [self.navigationController pushViewController:all animated:NO];
    [all release];
}



- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    UIImage *image = change[NSKeyValueChangeNewKey];
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    UIImageView *imageView = (UIImageView *)object;
    imageView.size = CGSizeMake(kScreen_width, kScreen_width *height/width);
    imageView.y = (kScreen_height - kScreen_width *height/width)/2;
    
    [imageView removeObserver:self forKeyPath:@"image"];
}


/**
 *  黑色蒙版
 */

-(void)blackView:(UIGestureRecognizer *)gest{
    UIView *blackView = [[UIView alloc] initWithFrame:self.view.bounds];
    UIImageView *imageView = (UIImageView *)gest.view;
    NSLog(@"%@",imageView);
    UIImage *image = [imageView.image copy];
    NSLog(@"--%@",image);
    UIImageView *X = [[UIImageView alloc] initWithImage:image];
    X.frame = imageView.frame;
    X.x = 0;
    [blackView addSubview:X];
    [X release];
    blackView.backgroundColor = [UIColor blackColor];
    //    NSLog(@"1%@",blackView);
    [self.navigationController.view addSubview:blackView];
    UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeBlack:)];
    [blackView addGestureRecognizer:tapGest];
    [tapGest release];
    [blackView release];
}

- (void)removeBlack:(UIGestureRecognizer *)gest{
    UIView *view = gest.view;
    [view removeFromSuperview];
}

@end
